package com.umeng.analytics.social;

import android.os.Build;
import android.text.TextUtils;

import com.umeng.analytics.pro.by;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.zip.GZIPInputStream;
import java.util.zip.InflaterInputStream;

public abstract class b {
    public b() {

    }

    protected static String a(String arg8) {
        InputStream v2_3 = null;
        String v2_2;
        InputStream v3;
        int v2;
        HttpURLConnection conn = null;
        String v5;
        String v1 = null;
        int v4 = new Random().nextInt(1000);
        try {
            v5 = System.getProperty("line.separator");
            if (arg8.length() <= 1) {
                by.e(v4 + ":  Invalid baseUrl.");
                String v0_2 = v1;
                return v0_2;
            }

            conn = (HttpURLConnection) new URL(arg8).openConnection();
            v2 = 10000;
            conn.setConnectTimeout(v2);
            conn.setReadTimeout(20000);
            conn.setRequestMethod("GET");
            if (Integer.parseInt(Build.VERSION.SDK) < 8) {
                System.setProperty("http.keepAlive", "false");
            }

            by.c(v4 + ": GET_URL: " + arg8);
            if (((HttpURLConnection) conn).getResponseCode() != 200) {
                by.c(v4 + ":  Failed to get message." + arg8);
                if (conn != null) {
                    conn.disconnect();
                }
                return v1;
            }

            v3 = ((HttpURLConnection) conn).getInputStream();
            v2_2 = ((HttpURLConnection) conn).getHeaderField("Content-Encoding");
            if ((TextUtils.isEmpty(((CharSequence) v2_2))) || !v2_2.equalsIgnoreCase("gzip")) {
                if (!TextUtils.isEmpty(((CharSequence) v2_2)) && (v2_2.equalsIgnoreCase("deflate"))) {
                    by.c(v4 + "  Use InflaterInputStream get data....");
                    v2_3 = new InflaterInputStream(v3);

                } else {
                    v2_3 = v3;
                }
            } else {
                by.c(v4 + "  Use GZIPInputStream get data....");
                v2_3 = new GZIPInputStream(v3);
            }
            v2_2 = b.a(v2_3);
            by.c(v4 + ":  response: " + v5 + v2_2);
            if (v2_2 != null) {
                return v2_2;
            }
            return v1;
        } catch (Exception v2_1) {

        } finally {
            try {
                if (conn != null) {
                    conn.disconnect();
                }
            } catch (Exception e) {

            }

            try {
                if (v2_3 != null) {
                    v2_3.close();
                }
            } catch (Exception e) {

            }
        }
        return v1;
    }

    protected static String a(String arg8, String arg9) {
        URLConnection v1_2;
        String v2_3;
        InputStream v3_1;
        int v2;
        URLConnection v0_2 = null;
        String v5;
        String v1 = null;
        InputStream v2_5 = null;
        int v4 = new Random().nextInt(1000);
        try {
            v5 = System.getProperty("line.separator");
            v0_2 = new URL(arg8).openConnection();
            v2 = 10000;
            v0_2.setConnectTimeout(v2);
            ((HttpURLConnection) v0_2).setReadTimeout(20000);
            ((HttpURLConnection) v0_2).setDoOutput(true);
            ((HttpURLConnection) v0_2).setDoInput(true);
            ((HttpURLConnection) v0_2).setUseCaches(false);
            ((HttpURLConnection) v0_2).setRequestMethod("POST");
            by.c(v4 + ": POST_URL: " + arg8);
            if (Integer.parseInt(Build.VERSION.SDK) < 8) {
                System.setProperty("http.keepAlive", "false");
            }

            if (!TextUtils.isEmpty(((CharSequence) arg9))) {
                by.c(v4 + ": POST_BODY: " + arg9);
                ArrayList v2_2 = new ArrayList();
                ((List) v2_2).add("data=" + arg9);
                OutputStream v3 = ((HttpURLConnection) v0_2).getOutputStream();
                v3.write(URLEncoder.encode(v2_2.toString()).getBytes());
                v3.flush();
                v3.close();
            }

            if (((HttpURLConnection) v0_2).getResponseCode() != 200) {
                by.e(v4 + ":  Failed to send message." + arg8);
                if (v0_2 == null) {
                    return v1;
                }
                ((HttpURLConnection) v0_2).disconnect();
                return v1;
            }

            v3_1 = ((HttpURLConnection) v0_2).getInputStream();
            v2_3 = ((HttpURLConnection) v0_2).getHeaderField("Content-Encoding");

            if (TextUtils.isEmpty(((CharSequence) v2_3))) {
                v2_5 = v3_1;
            } else if (v2_3.equalsIgnoreCase("gzip")) {
                v2_5 = new InflaterInputStream(v3_1);
            } else {
                v2_5 = v3_1;
            }
            v2_3 = b.a(v2_5);
            by.c(v4 + ":  response: " + v5 + v2_3);
            if (v2_3 != null) {
                label_90:
                if (v0_2 != null) {
                    ((HttpURLConnection) v0_2).disconnect();
                }

                return v2_3;
            }
            if (v0_2 != null) {
                ((HttpURLConnection) v0_2).disconnect();
            }

            String v0_3 = v1;
            return v0_3;
        } catch (Exception v2_1) {

        } finally {
            if (v0_2 != null) {
                try {
                    ((HttpURLConnection) v0_2).disconnect();
                } catch (Exception e) {

                }
            }

            if (v2_5 != null) {
                try {
                    v2_5.close();
                } catch (Exception e) {

                }
            }
        }
        return v1;
    }

    private static String a(InputStream arg5) {
        String v0 = null;
        BufferedReader v1 = new BufferedReader(new InputStreamReader(arg5), 8192);
        StringBuilder v2 = new StringBuilder();
        try {
            while (true) {
                String v3 = v1.readLine();
                if (v3 == null) {
                    break;
                }
                v2.append(v3 + "\n");
            }
        } catch (IOException v1_2) {
        } finally {
            try {
                arg5.close();
            } catch (IOException v1_2) {
                return v0;
            }
        }
        return v2.toString();
    }
}

